package com.action;

import com.hdb.*;
import com.hdb.dao.Types;
import com.util.OrderItem;
import com.util.Util;
import org.apache.struts2.convention.annotation.*;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author aszer
 */

/**
 * @author aszer
 */

@ParentPackage("default")
@Namespace("/order")
@Results({
        @Result(location = "/admin/order/orderManage.jsp"),
        @Result(name = "succeed", location = "/common/succeed.jsp"),
        @Result(name = "edit", location = "/admin/order/orderEditPage.jsp"),
        @Result(name = "finance", location = "/admin/order/financeStatistics.jsp"),
        @Result(name = "orderInfo", location = "/view/order/orderDetail.jsp"),
        @Result(name = "orderManage", location = "/admin/order/orderManage.jsp"),
        @Result(name = "orderDetail", location = "/view/order/orderDetail.jsp"),
})
@InterceptorRef("adminStack")/*设置过滤器*/
public class AdminOrderAction extends BasicAction {
    private int goodId;
    private int orderId;
    private DB db;
    private String message;
    private String path;
    private String fromDate;
    private String toDate;

    /*
    * 管理员查看未确认订单
    * */
    @Action("orderNew")
    public String orderNew() {
        int[] p = getP();
        List orderList = db.orderDAO.findOrderBy("orderState", 0, "orderDate", true, p[0], p[1]);
        getRequest().put("orderList", orderList);
        getPLink(orderList.size());
        return "orderManage";
    }

    /*
    * 管理员查看待发货订单
    * */
    @Action("orderSend")
    public String orderSend() {
        int[] p = getP();
        List orderList = db.orderDAO.findOrderBy("orderState", 1, "orderDate", true, p[0], p[1]);
        getRequest().put("orderList", orderList);
        getPLink(orderList.size());
        return "orderManage";
    }

    /*
    * 管理员查看历史订单
    * */
    @Action("orderOld")
    public String orderOld() {
        int[] p = getP();
        List orderList = db.orderDAO.findOrderBy("orderState", 2, "orderDate", true, p[0], p[1]);
        getRequest().put("orderList", orderList);
        getPLink(orderList.size());
        return "orderManage";
    }

    /*
    * 管理员查看所有订单
    * */
    @Action("orderManage")
    public String orderManager() {
        int[] p = getP();
        List orderList = db.orderDAO.findAllOrderBy("orderState", false, p[0], p[1]);
        getRequest().put("orderList", orderList);
        getPLink(orderList.size());
        return "orderManage";
    }

    /*
     * 查看订单详情
     */
    @Action("adminOrderDetail")
    public String adminOrderDetail() {
        int[] p = getP();
        List<Object[]> goodlist = db.orderGoodDAO.join(db.goodDAO, "goodId", "orderId", orderId, p[0], p[1]);
        List<OrderItem> orderItemList = new ArrayList<>();
        for (Object[] item : goodlist) {
            OrderItem oi = new OrderItem((TGood) item[1], ((TOrderGood) item[0]).getCount());
            orderItemList.add(oi);
        }
        getRequest().put("orderItemList", orderItemList);
        getPLink(orderItemList.size());
        return "orderDetail";
    }

    /*
     * 管理员或员工取消用户的订单
     */
    @Action("orderDelByAd")
    public String orderDelByAd() {
        TMyOrder order = db.orderDAO.findById(orderId);
        if (order == null) {
            setMessage("订单不存在");
        }
        if (order.getOrderState() != 0) {
            setMessage("不能取消已受理订单");
        } else {
            order.setOrderState(Types.order.Delete);
            //撤销库存修改
            List<Object[]> goodList = db.orderGoodDAO.join(db.goodDAO, "goodId", "orderId", order.getOrderId(), 0, 100);
            for (Object[] g : goodList) {
                TOrderGood og = (TOrderGood) g[0];
                TGood good = (TGood) g[1];
                db.goodInventoryRestore(good.getGoodId(), og.getCount());
            }
            db.orderDAO.attach(order);
        }
        this.setPath("order/orderManage.action");
        return "succeed";
    }

    /*
     * 管理员或员工删除用户的订单
     * 返回网页提示删除结果
     */
    @Action("orderRealDelByAd")
    public String orderRealDelByAd() {
        int res = db.orderDelById(orderId);
        this.setMessage("删除失败");
        if (res > 0) {
            this.setMessage("删除成功");
        }
        this.setPath("order/orderManage.action");
        return "succeed";
    }


    /*
     * 管理员或者员工在订单列表中受理订单
     */
    @Action("orderAccept")
    public String orderAccept() {
        TMyOrder order = db.orderDAO.findById(orderId);
        if (order == null) {
            exception("订单不存在", "order/orderManage.action");
        }
        order.setOrderState(Types.order.Accept);//已经受理订单
        db.orderDAO.attach(order);
        this.setMessage("受理订单成功");
        this.setPath("order/orderNew.action");
        return "succeed";
    }

    /*
     * 管理员或者员工在订单列表中完成订单
     */
    @Action("orderFinish")
    public String orderFinish() {
        TMyOrder order = db.orderDAO.findById(orderId);
        if (order == null) {
            exception("订单不存在", "order/orderManage.action");
        }
        order.setOrderState(Types.order.Finish);//完成订单
        setMessage(getResultInfo(
                db.orderDAO.attach(order)
        ));
        setPath("order/orderSend.action");
        return "succeed";
    }

    /*
     * 查看订单详情
     */
    @Action("orderInfo")
    public String orderInfo() {
        int[] p = getP();
        List<Object[]> goodlist = db.orderGoodDAO.join(db.goodDAO, "goodId", "orderId", orderId, p[0], p[1]);
        List<OrderItem> orderItemList = new ArrayList<>();
        for (Object[] item : goodlist) {
            OrderItem oi = new OrderItem((TGood) item[1], ((TOrderGood) item[0]).getCount());
            orderItemList.add(oi);
        }
        getRequest().put("orderItemList", orderItemList);
        getPLink(orderItemList.size());
        return "orderInfo";
    }
    /*统计*/
    @Action("financeStatistics")
    public String financeStatistics() {
        int p[] = getP();
        List orderList;
        try {
            orderList = db.financeStatistics(
                    new Timestamp(Util.YMDDate(fromDate).getTime()),
                    new Timestamp(Util.YMDDate(toDate).getTime() + 1 * 24 * 60 * 60 * 1000),
                    p[0],
                    p[1]
            );
        } catch (ParseException e) {
            e.printStackTrace();
            this.setMessage("日期格式不正确");
            this.setPath("order/orderManage.action");
            return "succeed";
        }
        getRequest().put("orderList", orderList);
        getPLink(orderList.size());
        return "finance";
    }

    public String getFromDate() {
        return fromDate;
    }

    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }

    public String getToDate() {
        return toDate;
    }

    public void setToDate(String toDate) {
        this.toDate = toDate;
    }

    public int getGoodId() {
        return goodId;
    }

    public void setGoodId(int goodsId) {
        this.goodId = goodsId;
    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public DB getDb() {
        return db;
    }

    public void setDb(DB db) {
        this.db = db;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

}
